package com.yochi.pictureseditor.activities;

import com.yochi.pictureseditor.R;
import com.yochi.pictureseditor.tools.Color;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class BichromePicker extends Activity {
	
	public static final String TAG = "BichromePicker";
	
	public static final int MAX_COLOR_VALUE = 100;

	private Intent resultIntent = new Intent();
	
	private TextView textViewHead;
	private TextView tvColor1;
	private TextView tvColor2;
	private SeekBar sbColor1;
	private SeekBar sbColor2;
	private SeekBar sbParameter;
	private Button okButton;
	private Button cancelButton;
	
	private Color color1;
	private Color color2;
	private int parameter;
	
    @Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(TAG,"::onCreate /BichromePicker.java/");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bichrome_picker);
     
        // get views
        textViewHead = (TextView) findViewById(R.id.textview_color);
        tvColor1 = (TextView) findViewById(R.id.textView_color_1);
        tvColor2 = (TextView) findViewById(R.id.textView_color_2);
        sbColor1 = (SeekBar) findViewById(R.id.seekbar_color_1);
        sbColor2 = (SeekBar) findViewById(R.id.seekbar_color_2);
        sbParameter = (SeekBar) findViewById(R.id.seekbar_parameter);
    	okButton = (Button) findViewById(R.id.button_color_ok);
    	cancelButton = (Button) findViewById(R.id.button_color_cancel);
    	
    	// set default values
    	sbColor1.setProgress(30);
    	color1 = computeColor(30);
    	tvColor1.setBackgroundColor(color1.getHexaColor());
    	sbColor2.setProgress(70);
    	color2 = computeColor(70);
    	tvColor2.setBackgroundColor(color2.getHexaColor());
    	parameter = 30;
    	sbParameter.setProgress(parameter);
        
        // get extras
        Bundle bundle = getIntent().getExtras();
        if(bundle!=null) {
        	textViewHead.setText(bundle.getString("name"));
        	resultIntent.putExtra("effect", bundle.getSerializable("effect"));
        	sbColor1.setMax(MAX_COLOR_VALUE);
        	sbColor2.setMax(MAX_COLOR_VALUE);
        	sbParameter.setMax(bundle.getInt("max"));
        }
        else textViewHead.setText(R.string.parameter_value);
        
        // red seekbar listener
        sbColor1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        	public void onStopTrackingTouch(SeekBar arg0) {}
        	public void onStartTrackingTouch(SeekBar arg0) {}
        	public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
        		color1 = computeColor(progress);
        		tvColor1.setBackgroundColor(color1.getHexaColor());
        }	});
        
        // green seekbar listener
        sbColor2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        	public void onStopTrackingTouch(SeekBar arg0) {}
        	public void onStartTrackingTouch(SeekBar arg0) {}
        	public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
        		color2 = computeColor(progress);
        		tvColor2.setBackgroundColor(color2.getHexaColor());
        }	});
        
        // blue seekbar listener
        sbParameter.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        	public void onStopTrackingTouch(SeekBar arg0) {}
        	public void onStartTrackingTouch(SeekBar arg0) {}
        	public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
        		parameter = progress;
        }	});
        
        // validate button listener
        okButton.setOnClickListener(new Button.OnClickListener() {  
	        public void onClick(View v) {
	        	resultIntent.putExtra("color1", color1);
	        	resultIntent.putExtra("color2", color2);
	        	resultIntent.putExtra("parameter", parameter);
	        	setResult(RESULT_OK, resultIntent);
	        	onDestroy();
	    }	});
        
        // cancel button listener
        cancelButton.setOnClickListener(new Button.OnClickListener() {  
	        public void onClick(View v) {
	        	setResult(RESULT_CANCELED, resultIntent);
	        	onDestroy();
	    }	});
	}
    
    
    public Color computeColor(int progress) {
    	int color1 = 0, color2 = 0;
    	float p = (float)progress;
    	
    	/* black to red */
    	if(progress <= 10) {
    		color1 = 0;
    		color2 = 0xff0000;  
    		p = progress / 10.0f;
    		
    	/* red to yellow */
    	} else if(progress <= 25) {
    		color1 = 0xff0000;
    		color2 = 0xffff00;  
    		p = (progress - 10) / 15.0f;
    		
    	/* yellow to lime green */
    	} else if(progress <= 40) {
    		color1 = 0xffff00;
    		color2 = 0x00ff00;  
    		p = (progress - 25) / 15.0f;
    		
    	/* lime green to aqua */
    	} else if(progress <= 55) {
    		color1 = 0x00ff00;
    		color2 = 0x00ffff;  
    		p = (progress - 40) / 15.0f;
    		
    	/* aqua to blue */
    	} else if(progress <= 70) {
    		color1 = 0x00ffff;
    		color2 = 0x0000ff;  
    		p = (progress - 55) / 15.0f;
    		
    	/* blue to fuchsia */
    	} else if(progress <= 90) {
    		color1 = 0x0000ff;
    		color2 = 0x00ff00;  
    		p = (progress - 70) / 20.0f;
    		
    	/* fuchsia to white */
    	} else if(progress <= 98) {
    		color1 = 0x00ff00;
    		color2 = 0xff00ff;  
    		p = (progress - 90) / 8.0f;
    		
    	} else {
    		color1 = 0xffffff;
    		color2 = 0xffffff;
    		p = 1.0f;
    	}

    	int r1 = (color1 >> 16) & 0xff;
    	int r2 = (color2 >> 16) & 0xff;
    	int g1 = (color1 >> 8) & 0xff;
    	int g2 = (color2 >> 8) & 0xff;
    	int b1 = (color1) & 0xff;
    	int b2 = (color2) & 0xff;

    	char r3 = (char) ((r2 * p) + (r1 * (1.0f-p)));
    	char g3 = (char) (g2 * p + g1 * (1.0f-p));
    	char b3 = (char) (b2 * p + b1 * (1.0f-p));

    	//int color = r3 << 16 | g3 << 8 | b3;

    	return new Color(r3, g3, b3);
    }
    
    @Override
    public void onDestroy() {
    	super.onDestroy();
    	finish();
    }
}
